package com.ruoyi.web.controller.workJob;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.worker.domain.TermAttendanceGroup;
import com.ruoyi.worker.domain.TermAttendanceRules;
import com.ruoyi.worker.domain.TimeShift;
import com.ruoyi.worker.service.TermAttendanceGroupService;
import com.ruoyi.worker.service.TimeShiftService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能描述:考勤组管理控制层
 *
 * @param
 * @param
 * @author admin
 * @Date 2021/11/12 10:28
 * @return
 */

@RestController
@RequestMapping("/workJobApi/termAttendanceGroup")
public class TermAttendanceGroupController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(TermAttendanceGroupController.class);
    @Autowired
    private TermAttendanceGroupService termAttendanceGroupService;
    @Autowired
    private TimeShiftService timeShiftService;

    /**
     * 功能描述: 考勤组查询
     *
     * @param
     * @param termAttendanceGroup
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Date 2021/11/12 10:38
     * @author 刘松山
     */
    @GetMapping("/listTermAttendanceGroup")
    public TableDataInfo listTermAttendanceGroup(TermAttendanceGroup termAttendanceGroup) {
        startPage();
        List<TermAttendanceGroup> list = termAttendanceGroupService.listTermAttendanceGroup(termAttendanceGroup);

        for (TermAttendanceGroup t : list) {
            String showTime = "";
            if (t.getType() == 1) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime();
                t.setTypeName("一天1次上下班打卡");
            } else if (t.getType() == 2) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime();
                t.setTypeName("一天2次上下班打卡");
            } else if (t.getType() == 3) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime()
                        + "第3次上下班： " + t.getThreeBeginTime() + "-" + t.getThreeEndTime();
                t.setTypeName("一天3次上下班打卡");
            } else if (t.getType() == 4) {
                showTime = "第1次上下班： " + t.getFirstBeginTime() + "-" + t.getFirstEndTime()
                        + "第2次上下班： " + t.getSecondBeginTime() + "-" + t.getSecondEndTime()
                        + "第3次上下班： " + t.getThreeBeginTime() + "-" + t.getThreeEndTime()
                        + "第4次上下班： " + t.getFourBeginTime() + "-" + t.getFourEndTime();
                t.setTypeName("一天4次上下班打卡");
            }


            t.setShowDateTime(showTime);
        }
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('job:attendanceGroup:query')")
    @GetMapping(value = {"/", "/{id}"})
    public AjaxResult getTermAttendanceGroup(@PathVariable(value = "id", required = false) Long id) {
        AjaxResult ajax = AjaxResult.success();

        if (StringUtils.isNotNull(id)) {
            ajax.put(AjaxResult.DATA_TAG, termAttendanceGroupService.getTermAttendanceGroup(id));

        }
        return ajax;
    }

    //  @PreAuthorize("@ss.hasPermi('workJobApi:attendance:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addTermAttendanceGroup(@Validated @RequestBody TermAttendanceGroup termAttendanceGroup) {

        termAttendanceGroup.setCreateBy(getUsername());
        int flag = termAttendanceGroupService.addTermAttendanceGroup(termAttendanceGroup);
        if (flag < 0) {
            return AjaxResult.error(termAttendanceGroup.getName() + "的班次已经存在了");
        }
        return toAjax(flag);
    }


    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TermAttendanceGroup termAttendanceGroup) {

        termAttendanceGroup.setUpdateBy(getUsername());
        int flag = termAttendanceGroupService.modifTermAttendanceGroup(termAttendanceGroup);
        if (flag < 0) {
            return AjaxResult.error(termAttendanceGroup.getName() + "的班次已经存在了");
        }
        return toAjax(flag);
    }

    @Log(title = "修改状态", businessType = BusinessType.UPDATE)
    @PostMapping("/modifTermAttendanceGroupStatus")
    public AjaxResult modifTermAttendanceGroupStatus(@Validated @RequestBody TermAttendanceGroup termAttendanceGroup) {

        termAttendanceGroup.setUpdateBy(getUsername());
        int flag = termAttendanceGroupService.modifTermAttendanceGroupStatus(termAttendanceGroup);

        return toAjax(flag);
    }

    /**
     * 删除
     */
    //@PreAuthorize("@ss.hasPermi('job:attendanceGroup:detail:delete')")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delBactchTermAttendanceGroup(@PathVariable Long[] ids) {
        return toAjax(termAttendanceGroupService.delBactchTermAttendanceGroup(ids));
    }

    /**
     * 功能描述:h获取班次
     *
     * @param
     * @param timeShift
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Date 2021/11/12 10:55
     * @author 刘松山
     */
    @GetMapping("/listTimeShiftList")
    public TableDataInfo getCompanyListInfo(TimeShift timeShift) {

        List<TimeShift> timeShiftList = timeShiftService.getTimeShiftList(timeShift);
        return getDataTable(timeShiftList);
    }

    /**
     * 功能描述:获取长期工考勤迟到 早退 旷工规则
     *
     * @param
     * @param
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Date 2021/11/12 11:10
     * @author 刘松山
     */
    @GetMapping(value = {"/getTermAttendanceRules"})
    public AjaxResult getTermAttendanceRules() {
        AjaxResult ajax = AjaxResult.success();

        ajax.put(AjaxResult.DATA_TAG, termAttendanceGroupService.getTermAttendanceRules());


        return ajax;
    }

    //  @PreAuthorize("@ss.hasPermi('workJobApi:attendance:add')")
    @Log(title = "新增考勤规则", businessType = BusinessType.INSERT)
    @PostMapping("/addTermAttendanceRules")
    public AjaxResult addTermAttendanceRules(@Validated @RequestBody TermAttendanceRules termAttendanceRules) {

        termAttendanceRules.setCreateBy(getUsername());
        int flag = termAttendanceGroupService.addTermAttendanceRules(termAttendanceRules);

        return toAjax(flag);
    }
}
